load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

PROGRAMS = [
    # TODO: add top, ps
    "free",
    "hugetop",
    "kill",
    "pgrep",
    "pidof",
    "pmap",
    "pwdx",
    "skill",
    "slabtop",
    "sysctl",
    "tload",
    "uptime",
    "vmstat",
    "w",
    "watch",
]

LOCAL_DEFINES = [
    "BUILD_WITH_WHINE=1",
    "ENABLE_NLS=1",
    "ENABLE_PIDWAIT=1",
    "HAVE_ALARM=1",
    "HAVE_ARPA_INET_H=1",
    "HAVE_ATEXIT=1",
    "HAVE_CLOCK_GETTIME=1",
    "HAVE_DCGETTEXT=1",
    "HAVE_DECL___UT_HOSTSIZE=1",
    "HAVE_DLFCN_H=1",
    "HAVE_DUP2=1",
    "HAVE_ERROR_H=1",
    "HAVE_FCNTL_H=1",
    "HAVE_FLOAT_H=1",
    "HAVE_FORK=1",
    "HAVE_GETHOSTNAME=1",
    "HAVE_GETPAGESIZE=1",
    "HAVE_GETTEXT=1",
    "HAVE_GETTIMEOFDAY=1",
    "HAVE_INTTYPES_H=1",
    "HAVE_ISWPRINT=1",
    "HAVE_LANGINFO_H=1",
    "HAVE_LIBINTL_H=1",
    "HAVE_LIMITS_H=1",
    "HAVE_LOCALE_H=1",
    "HAVE_MALLOC=1",
    "HAVE_MBRTOWC=1",
    "HAVE_MEMCHR=1",
    "HAVE_MEMMOVE=1",
    "HAVE_MEMSET=1",
    "HAVE_MKDIR=1",
    "HAVE_MMAP=1",
    "HAVE_NCURSES=1",
    "HAVE_NCURSESW_NCURSES_H=1",
    "HAVE_NL_LANGINFO=1",
    "HAVE_PROGRAM_INVOCATION_NAME=1",
    "HAVE_PROGRAM_INVOCATION_SHORT_NAME=1",
    "HAVE_PUTENV=1",
    "HAVE_REALLOC=1",
    "HAVE_REGCOMP=1",
    "HAVE_RPMATCH=1",
    "HAVE_SELECT=1",
    "HAVE_SETLOCALE=1",
    "HAVE_SIGABBREV_NP=1",
    "HAVE_SIGINFO_T_SI_INT=1",
    "HAVE_STDINT_H=1",
    "HAVE_STDIO_EXT_H=1",
    "HAVE_STDIO_H=1",
    "HAVE_STDLIB_H=1",
    "HAVE_STRCASECMP=1",
    "HAVE_STRCHR=1",
    "HAVE_STRCOLL=1",
    "HAVE_STRCSPN=1",
    "HAVE_STRDUP=1",
    "HAVE_STRERROR=1",
    "HAVE_STRINGS_H=1",
    "HAVE_STRING_H=1",
    "HAVE_STRNCASECMP=1",
    "HAVE_STRNDUP=1",
    "HAVE_STRPBRK=1",
    "HAVE_STRRCHR=1",
    "HAVE_STRSPN=1",
    "HAVE_STRSTR=1",
    "HAVE_STRTOL=1",
    "HAVE_STRTOUL=1",
    "HAVE_STRUCT_STAT_ST_RDEV=1",
    "HAVE_STRVERSCMP=1",
    "HAVE_SYS_FILE_H=1",
    "HAVE_SYS_IOCTL_H=1",
    "HAVE_SYS_PARAM_H=1",
    "HAVE_SYS_STAT_H=1",
    "HAVE_SYS_TIME_H=1",
    "HAVE_SYS_TYPES_H=1",
    "HAVE_TERMIOS_H=1",
    "HAVE_UNISTD_H=1",
    "HAVE_UTMPNAME=1",
    "HAVE_UTMPX_H=1",
    "HAVE_UTMP_H=1",
    "HAVE_UT_HOSTSIZE_IN_UTMPX=1",
    "HAVE_VALUES_H=1",
    "HAVE_VFORK=1",
    "HAVE_WCHAR_H=1",
    "HAVE_WCTYPE_H=1",
    "HAVE_WCWIDTH=1",
    "HAVE_WORKING_FORK=1",
    "HAVE_WORKING_VFORK=1",
    "HAVE__BOOL=1",
    "HAVE___FPENDING=1",
    "HAVE___PROGNAME=1",
    'LT_OBJDIR=\\".libs/\\"',
    "MAJOR_IN_SYSMACROS=1",
    'PACKAGE=\\"' + module_name() + '\\"',
    'PACKAGE_BUGREPORT=\\"procps@freelists.org\\"',
    'PACKAGE_NAME=\\"' + module_name() + '\\"',
    'PACKAGE_STRING=\\"' + module_name() + "=" + module_version() + '\\"',
    'PACKAGE_TARNAME=\\"' + module_name() + '\\"',
    'PACKAGE_URL=\\"https://gitlab.com/procps-ng/procps\\"',
    'PACKAGE_VERSION=\\"' + module_version() + '\\"',
    "STDC_HEADERS=1",
    'VERSION="' + module_version() + '"',
    "_GNU_SOURCE",
]

genrule(
    name = "gen_config_h",
    outs = ["config.h"],
    cmd = "touch $@",
)

cc_library(
    name = "config",
    hdrs = ["config.h"],
    local_defines = LOCAL_DEFINES,
)

cc_library(
    name = "library",
    srcs = glob(["library/*.c"]),
    hdrs = glob(["library/include/*.h"]),
    includes = ["library/include"],
    local_defines = LOCAL_DEFINES,
    deps = [":local"],
)

cc_library(
    name = "local",
    srcs = glob(["local/*.c"]),
    hdrs = glob(["local/*.h"]),
    includes = ["local"],
    local_defines = LOCAL_DEFINES,
    deps = [":config"],
)

EXTRA_DEPS = {
    "watch": ["@ncurses"],
    "hugetop": ["@ncurses"],
    "slabtop": ["@ncurses"],
}

[
    cc_binary(
        name = prog,
        srcs = ["src/%s.c" % prog],
        local_defines = LOCAL_DEFINES,
        visibility = ["//visibility:public"],
        deps = [
            ":library",
            ":local",
        ] + EXTRA_DEPS.get(prog, []),
    )
    for prog in PROGRAMS
]
